package com.ruoyi.test_tree.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.test_tree.domain.SysTreeDict;
import com.ruoyi.test_tree.service.ISysTreeDictService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 系统树形字典Controller
 * 
 * @author ruoyi
 * @date 2025-08-21
 */
@RestController
@RequestMapping("/test/tree_dict")
public class SysTreeDictController extends BaseController
{
    @Autowired
    private ISysTreeDictService sysTreeDictService;

    /**
     * 查询系统树形字典列表
     */
    @PreAuthorize("@ss.hasPermi('test:tree_dict:list')")
    @GetMapping("/list")
    public AjaxResult list(SysTreeDict sysTreeDict)
    {
        List<SysTreeDict> list = sysTreeDictService.selectSysTreeDictList(sysTreeDict);
        return success(list);
    }

    /**
     * 导出系统树形字典列表
     */
    @PreAuthorize("@ss.hasPermi('test:tree_dict:export')")
    @Log(title = "系统树形字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTreeDict sysTreeDict)
    {
        List<SysTreeDict> list = sysTreeDictService.selectSysTreeDictList(sysTreeDict);
        ExcelUtil<SysTreeDict> util = new ExcelUtil<SysTreeDict>(SysTreeDict.class);
        util.exportExcel(response, list, "系统树形字典数据");
    }

    /**
     * 获取系统树形字典详细信息
     */
    @PreAuthorize("@ss.hasPermi('test:tree_dict:query')")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable("dictId") Long dictId)
    {
        return success(sysTreeDictService.selectSysTreeDictByDictId(dictId));
    }

    /**
     * 新增系统树形字典
     */
    @PreAuthorize("@ss.hasPermi('test:tree_dict:add')")
    @Log(title = "系统树形字典", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTreeDict sysTreeDict)
    {
        return toAjax(sysTreeDictService.insertSysTreeDict(sysTreeDict));
    }

    /**
     * 修改系统树形字典
     */
    @PreAuthorize("@ss.hasPermi('test:tree_dict:edit')")
    @Log(title = "系统树形字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTreeDict sysTreeDict)
    {
        return toAjax(sysTreeDictService.updateSysTreeDict(sysTreeDict));
    }

    /**
     * 删除系统树形字典
     */
    @PreAuthorize("@ss.hasPermi('test:tree_dict:remove')")
    @Log(title = "系统树形字典", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        return toAjax(sysTreeDictService.deleteSysTreeDictByDictIds(dictIds));
    }

    /**
     * 获取树形结构数据
     */
    @GetMapping("/tree")
    public AjaxResult tree(@RequestParam(required = false) String dictValueStartWith)
    {
        List<SysTreeDict> list = sysTreeDictService.selectTreeDictTree(dictValueStartWith);
        return success(list);
    }
}
